<?php


namespace App\Http\Controllers\Admin\Family;


use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;

//首页
class FamilyController extends BaseController
{
    public function list(Request $request)
    {
        $param = $request->only('start_time', 'end_time', 'code', 'id', 'nickname', 'user_name', 'user_id_number', 'page', 'page_size', 'name', 'user_code');
        $v = app('validator')->make($param, [
            'start_time' => 'filled|string',
            'end_time' => 'filled|string',
            'code' => 'filled|integer',
            'id' => 'filled|integer',
            'nickname' => 'filled|string|max:50',
            'user_name' => 'filled|string|max:50',
            'user_id_number' => 'filled|string|max:50',
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1|max:1000',
            'name' => 'string|max:255',
            'user_code' => 'filled|integer',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $param['role_id'] = $request->role_id;
        $list = app('repo_family')->list($param, $page, $pageSize);
        return Response::sendList($list);
    }

    /**
     * @Desc:详情
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/28 17:57
     */
    public function info(Request $request)
    {
        $param = $request->only('id');
        $v = app('validator')->make($param, [
            'id' => 'required|integer',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $param['role_id'] = $request->role_id;
        $data = app('repo_family')->info($param);
        return Response::sendData($data);
    }

    /**
     * @Desc:新建
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/28 17:57
     */
    public function create(Request $request)
    {
        $param = $request->only(['user_code', 'classify_id']);
        $v = app('validator')->make($param, [
            'user_code' => 'required|integer',
            'classify_id' => 'required|integer',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        app('repo_family')->create($param);
        return Response::sendData(true);
    }

    /**
     * @Desc:更新
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/28 17:58
     */
    public function update(Request $request)
    {
        $param = $request->only(['id', 'user_code', 'classify_id', 'icon', 'name']);
        $v = app('validator')->make($param, [
            'id' => 'required|integer',
            'user_code' => 'filled|string',
            'classify_id' => 'filled|integer',
            'icon' => 'filled|string|max:255',
            'name' => 'filled|string|max:10',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        app('repo_family')->update($param);
        return Response::sendData(true);
    }

    /**
     * @Desc:家族分类人数
     * @return string
     * @author: shiquan
     * @Time: 2022/11/28 18:01
     */
    public function classify()
    {
        $classify = app('repo_family')->familyClassify();
        return Response::sendData($classify);
    }

    /**
     * @Desc:根据用户code获取用户id
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/28 18:14
     */
    public function userInfo(Request $request)
    {
        $param = $request->only(['user_code']);
        $v = app('validator')->make($param, [
            'user_code' => 'required|integer',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $param['role_id'] = $request->role_id;
        $userInfo = app('repo_family')->userInfo($param);
        return Response::sendData($userInfo);
    }

    /**
     * @Desc:解散
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2022/11/29 9:58
     */
    public function delete(Request $request)
    {
        $param = $request->only(['id']);
        $v = app('validator')->make($param, [
            'id' => 'required|integer',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        $param['admin_id'] = $request->admin_id;
        $userInfo = app('repo_family')->delete($param);
        return Response::sendData($userInfo);
    }

    /**
     * @Desc:红包召集记录
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2023/1/31 11:07
     */
    public function redPacketList(Request $request)
    {
        $param = $request->only('start_time', 'end_time', 'code', 'user_code', 'room_code', 'status', 'page', 'page_size');
        $v = app('validator')->make($param, [
            'start_time' => 'nullable|string',
            'end_time' => 'nullable|string',
            'code' => 'nullable|string',
            'user_code' => 'nullable|string',
            'room_code' => 'nullable|string',
            'status' => 'nullable|integer|in:0,1,2',
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1|max:10000',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $list = app('repo_family')->redPacketList($param, $page, $pageSize);
        return Response::sendList($list);
    }

    /**
     * @Desc:红包召集详情
     * @param  Request  $request
     * @return string
     * @throws BasicException
     * @author: shiquan
     * @Time: 2023/1/31 11:26
     */
    public function redPacketInfo(Request $request)
    {
        $param = $request->only('page', 'page_size', 'id');
        $v = app('validator')->make($param, [
            'id' => 'required|integer',
            'page' => 'integer|min:1',
            'page_size' => 'integer|min:1|max:10000',
        ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $list = app('repo_family')->redPacketInfo($param, $page, $pageSize);
        return Response::sendData($list);
    }
}